package com.fengmang.myplugin;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.util.Log;

import java.io.File;
import java.lang.reflect.Method;

import dalvik.system.DexClassLoader;

public class PluginManager {
    
    private static final PluginManager ourInstance = new PluginManager();

    private DexClassLoader dexClassLoader;

    private Resources resources;

    private PackageInfo packageInfo;
    
    public static PluginManager getInstance(){
        return ourInstance;
    }

    public PluginManager() {
    }
    
    public void loadPath(Context context){
        File filesDir = context.getDir("plugin", Context.MODE_PRIVATE);
        String name = "app-debug.apk";
//        String name = "taopiaopiao-debug.apk";
        String path = new File(filesDir, name).getAbsolutePath();

//        String path = Environment.getExternalStorageDirectory()+ File.separator + "plugin.apk";

//        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/app-debug-f.apk";


//        String path = "/data/data/com.hongx.plugin/app_dex/classes.dex";
//        String path = "/data/data/com.fengmang.scouringpad/app_dex/classes.dex";

//        String path = "/data/data/com.fengmang.scouringpad/app_plugin/app-debug.apk";

        PackageManager packageManager = context.getPackageManager();
        packageInfo = packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);

//        for (int i=0;i<packageInfo.activities.length;i++){
//            Log.e("packageInfo===", packageInfo.activities[i].name);
//        }

        //com.hongx.taopiaopiao             TaoMainActivity
        Log.e("loadPath===", packageInfo+"==="+filesDir+"==="+path);

        //activity
        ///data/data/com.hongx.plugin/app_dex/plugin.dex


        //resource
        try {
//            File dex = context.getDir("dex", Context.MODE_PRIVATE);
            File patchDexDir = context.getDir("dex", Context.MODE_PRIVATE);
            File optimizedFile = new File(patchDexDir, "optimized");
            if (!optimizedFile.exists()) {
                optimizedFile.mkdirs();
            }
//            Log.e("loadPath===1", "==="+optimizedFile);
//            String path2 = optimizedFile.getAbsolutePath();
//            String path2 = "/data/data/com.fengmang.scouringpad/app_dex/optimized";
//            String path2 = "/data/data/com.hongx.plugin/app_dex/optimized";
//            dexClassLoader = new DexClassLoader(path, path2, null, context.getClassLoader());

//            File optimizedFile = context.getDir("dex", Context.MODE_PRIVATE);
            dexClassLoader = new DexClassLoader(path, optimizedFile.getAbsolutePath(), null, context.getClassLoader());


//            dexClassLoader.loadClass("com.hongx.taopiaopiao.TaoMainActivity");
//            dexClassLoader.loadClass("com.fengmang.scouringpad.PersonInfoActivity");
//            dexClassLoader.loadClass("com.fengmang.myplugin2.TaoMainActivity");
            dexClassLoader.loadClass("com.carcontrol.myplugin3.TaoMainActivity");

            AssetManager manager = AssetManager.class.newInstance();
            Method addAssetPath = AssetManager.class.getMethod("addAssetPath", String.class);
            addAssetPath.invoke(manager, path);
            resources = new Resources(manager, context.getResources().getDisplayMetrics(), context.getResources().getConfiguration());

            Log.e("loadPath===2", "==="+dexClassLoader);
        } catch (Exception e) {
            Log.e("loadPath===e", "==="+e);
        }
    }

    public Resources getResources(){
        return  resources;
    }

    public DexClassLoader getDexClassLoader(){
        return dexClassLoader;
    }

    public PackageInfo getPackageInfo(){
        return packageInfo;
    }
}
